//
//  CN_ShowHelper.swift
//  YiZhangGui
//
//  Created by 卢逢卫 on 2024/4/3.
//

import UIKit

public class CN_ShowHelper: NSObject {
}

extension CN_ShowHelper {

    public static func show(imageView: UIImageView) {
        guard let keyWindow = UIApplication.shared.keyWindow else {
            return
        }
        
        let containerView = UIView(frame: keyWindow.bounds)
        containerView.backgroundColor = .black
        containerView.alpha = 0
        
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(dismissImageView(_:)))
        containerView.addGestureRecognizer(tapGesture)
        
        let expandedImageView = UIImageView(frame: containerView.bounds)
        expandedImageView.image = imageView.image
        expandedImageView.contentMode = .scaleAspectFit
        expandedImageView.clipsToBounds = true
        expandedImageView.isUserInteractionEnabled = true
        
        containerView.addSubview(expandedImageView)
        keyWindow.addSubview(containerView)
        
        UIView.animate(withDuration: 0.3) {
            containerView.alpha = 1
        }
    }
    
    @objc private static func dismissImageView(_ sender: UITapGestureRecognizer) {
        guard let containerView = sender.view else {
            return
        }
        
        UIView.animate(withDuration: 0.3, animations: {
            containerView.alpha = 0
        }) { _ in
            containerView.removeFromSuperview()
        }
    }
}
